﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using T.ApiResults;
using T.Personnel.Domain;
using T.Personnel.Domain.Dto;
using T.Personnel.Read.API.Application.Command.EntryManageCommand;
using T.Personnel.Read.API.Application.Command.ManageCommand;

namespace T.Personnel.Read.API.Controllers
{
    /// <summary>
    /// 人事管理-入职办理-读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ReadEntryManageController : ControllerBase
    {
        private readonly IMediator mediator;

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="mediator"></param>
        public ReadEntryManageController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 入职办理列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Result<Manage>>> GetListEntryManageAsync([FromQuery]GetListEntryManageCommand command)
        {
            ApiResult<Result<Manage>> result = new ApiResult<Result<Manage>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 反填入职办理信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Manage>> GetEntryManageAsync([FromQuery]GetEntryManageCommand command)
        {
            ApiResult<Manage> result = new ApiResult<Manage>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 当月入职人数
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> GetLeavingCountForCurrentMonthAsync([FromQuery]GetEntryAllCommand command)
        {
            var gather = await mediator.Send(command);
            gather = gather.Where(x=>x.AppStatus=="已同意").ToList();
            // 获取当前月份
            var currentMonth = DateTime.Now.Month;
            var currentYear = DateTime.Now.Year;

            // 查询离职员工数量
            var leaveCount = gather
                .Where(m =>
                            m.Hiredate.Year == currentYear &&
                            m.Hiredate.Month == currentMonth)
                .Count();

            return leaveCount;
        }

        /// <summary>
        /// 审批入职办理列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Result<Manage>>> GetListEntrysManageAsync([FromQuery] GetListEntrysManageCommand command)
        {
            ApiResult<Result<Manage>> result = new ApiResult<Result<Manage>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }
    }
}
